package com.menu.web.controller.product;

import com.menu.common.annotation.Log;
import com.menu.common.core.controller.BaseController;
import com.menu.common.core.domain.AjaxResult;
import com.menu.common.core.domain.entity.MenuProtype;
import com.menu.common.enums.BusinessType;
import com.menu.common.enums.DataSourceType;
import com.menu.common.utils.poi.ExcelUtil;
import com.menu.framework.datasource.DynamicDataSourceContextHolder;
import com.menu.system.service.IMenuProtypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 产品类别Controller
 *
 * @author ruoyi
 * @date 2024-06-11
 */
@RestController
@RequestMapping("/system/protype")
public class MenuProtypeController extends BaseController {
    @Autowired
    private IMenuProtypeService menuProtypeService;

    /**
     * 查询产品类别列表
     */
    @PreAuthorize("@ss.hasPermi('system:protype:list')")
    @GetMapping("/list")
    public AjaxResult list(MenuProtype menuProtype) {
        List<MenuProtype> list = menuProtypeService.selectMenuProtypeList(menuProtype);
        return success(list);
    }

    /**
     * 导出产品类别列表
     */
    @PreAuthorize("@ss.hasPermi('system:protype:export')")
    @Log(title = "产品类别", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MenuProtype menuProtype) {
        List<MenuProtype> list = menuProtypeService.selectMenuProtypeList(menuProtype);
        ExcelUtil<MenuProtype> util = new ExcelUtil<MenuProtype>(MenuProtype.class);
        util.exportExcel(response, list, "产品类别数据");
    }

    /**
     * 获取产品类别详细信息
     */
//    @PreAuthorize("@ss.hasPermi('system:protype:query')")
    @GetMapping(value = "/{productId}")
    public AjaxResult getInfo(@PathVariable("productId") Long productId) {
        return success(menuProtypeService.selectMenuProtypeByProductId(productId));
    }

    /**
     * 新增产品类别
     */
    @PreAuthorize("@ss.hasPermi('system:protype:add')")
    @Log(title = "产品类别", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MenuProtype menuProtype) {


        if (menuProtypeService.selectMenuFindProductName(menuProtype) != null) {

            return AjaxResult.error(DynamicDataSourceContextHolder.getDataSourceType().equals(DataSourceType.MASTER.name()) ? "产品类别已存在" : "The product category already exists");
        }

        return toAjax(menuProtypeService.insertMenuProtype(menuProtype));
    }

    /**
     * 修改产品类别
     */
    @PreAuthorize("@ss.hasPermi('system:protype:edit')")
    @Log(title = "产品类别", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MenuProtype menuProtype) {
        if (menuProtype.getProductName().equals(menuProtypeService.selectMenuProtypeByProductId(menuProtype.getProductId()).getProductName())) {
            return toAjax(menuProtypeService.updateMenuProtype(menuProtype));

        } else {
            if (menuProtypeService.selectMenuFindProductName(menuProtype) != null) {
                return AjaxResult.error(DynamicDataSourceContextHolder.getDataSourceType().equals(DataSourceType.MASTER.name()) ? "产品类别已存在" : "The product category already exists");
            }
            return toAjax(menuProtypeService.updateMenuProtype(menuProtype));
        }


    }

    /**
     * 删除产品类别
     */
    @PreAuthorize("@ss.hasPermi('system:protype:remove')")
    @Log(title = "产品类别", businessType = BusinessType.DELETE)
    @DeleteMapping("/{productIds}")
    public AjaxResult remove(@PathVariable Long[] productIds) {

        if (menuProtypeService.deleteMenuProtypeByProductIds(productIds) == 0) {
            return AjaxResult.error(DynamicDataSourceContextHolder.getDataSourceType().equals(DataSourceType.MASTER.name()) ? "存在子节点删除失败" : "Child node deletion failed");
        }


        return AjaxResult.success();
    }
}
